import time

from selenium import webdriver
from selenium.webdriver.chrome.webdriver import WebDriver
from selenium.webdriver.common.by import By
from selenium.webdriver.remote.webelement import WebElement
from selenium.webdriver.support.select import Select

from page.basepage import BasePage


class ApplyPage(BasePage):

    # driver 浏览器驱动
    # by_type 定位元素类型
    # by_value  定位元素类型对应的值
    # send_value 输入的数据
    # def sendKey(self, driver, by_type, by_value, send_value):
    #     self.findElement(driver, by_type, by_value).send_keys(send_value)

    def sendKey(self, driver: WebDriver, by_type, by_value, send_value):
        name_et = self.findElement(driver, by_type, by_value)
        name_et.send_keys(send_value)

    def select(self, driver, by_type, by_value, send_value):
        if send_value != "" and send_value != None:
            xueli_et = self.findElement(driver, by_type, by_value)
            Select(xueli_et).select_by_visible_text(send_value)

    def click(self, driver, by_type, by_value):
        submit_et = self.findElement(driver, by_type, by_value)
        submit_et.click()

    def getResult(self, driver) -> str:
        alert = getAlert(driver, 60)
        result_text = alert.text
        alert.accept()
        return result_text

        # result_text = driver.switch_to.alert.text
        # driver.switch_to.alert.accept()
        # return result_text



def getAlert(driver: WebDriver, timeout):
    i = 0
    while True:
        if timeout == i:
            return False
        try:
            return driver.switch_to.alert
        except Exception:
            pass
        finally:
            time.sleep(1)
            i = i + 1
